/**
 * Copyright (C) 2009-2012, KingHool.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package com.kinghool.smodel.io.json;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import com.kinghool.ebos.commons.utils.Utils;
import com.kinghool.smodel.io.IModelChannel;

/**
 * 
 * @Author Allen Deng(dengtailin@gmail.com) 
 */
public class JsonChannel extends JsonStreamChannel implements IModelChannel {
	private String json;
	
	public JsonChannel(String json) {
		this.json = json;
	}
	
	public JsonChannel() {
		this(null);
	} 
	
	public String getMappedObject() {
		return json;
	} 
	
	public void setMappedObject(Object json) {
		this.json = json == null ? null : String.valueOf(json);
	}
	
	protected Reader getReader() {
		return new StringReader(Utils.getNotNullString(json));
	}
	
	protected Writer getWriter() {
		return new StringWriter();
	}

	protected void afterLoad(Writer writer) {
		json = ((StringWriter)writer).getBuffer().toString();
		super.afterLoad(writer);
	}
}
